package com.example.nirvana.dataStructure.algorithm;

import cn.hutool.json.JSONUtil;

/**
 * 简单排序算法
 */
public class SimpleSort {
    /**
     * 选择排序（升序）
     *
     * @param data 数组
     */
    public static void selectCompositorASC(int data[]) {
        int smaller;
        for (int i = 0; i < data.length; i++) {
            smaller = i;
            for (int j = i+1; j < data.length; j++) {
                if (data[smaller] > data[j]) {
                    smaller = j;
                }
            }

            if (smaller != i) {
                int temp = data[i];
                data[i] = data[smaller];
                data[smaller] = temp;
            }
        }
    }

    /**
     * 冒泡排序（升序）
     *
     * @param data 数组
     */
    public static void bubbleCompositorASC(int[] data) {
        for (int i = 0; i < data.length-1; i++) {
            for (int j = 0; j < data.length-i-1; j++) {
                if (data[j] > data[j+1]) {
                    int temp = data[j];
                    data[j] = data[j+1];
                    data[j+1] = temp;
                }
            }
        }
    }

    /**
     * 插入排序（升序）
     *
     * @param data 数组
     */
    public static void insertCompositorASC(int[] data) {
        int current;
        for (int i = 1; i < data.length; i++) {
            current = data[i];
            for (int j = i-1; j >= 0; j--) {
                if (current < data[j]) {
                    data[j+1] = data[j];
                }else {
                    data[j+1] = current;
                    break;
                }
                if (j == 0) {
                    data[j] = current;
                }
            }

        }
    }
}
